/*--------------

SITP core tool

--------------*/

TOOL.Category		= "Construction"
TOOL.Name			= "#SITP Core Tool"
TOOL.Command		= nil
TOOL.ConfigName		= ""

TOOL.ClientConVar[ "coretype" ] = "20 ton core"

cleanup.Register ( "sitp_core" )

cores = {}
coreids = {}

-- 5 t core
cores["20 ton core"] = {}
	cores["20 ton core"].Model = "models/Combine_Helicopter/helicopter_bomb01.mdl"
	cores["20 ton core"].Mass = 500
	cores["20 ton core"].TonGrade = 20
	cores["20 ton core"].id = "20 ton core"
	
	table.insert(coreids, cores["20 ton core"].id)
-- 25 t core
cores["100 ton core"] = {}
	cores["100 ton core"].Model = "models/props_wasteland/kitchen_stove002a.mdl"
	cores["100 ton core"].Mass = 2500
	cores["100 ton core"].TonGrade = 100
	cores["100 ton core"].id = "100 ton core"
	
	table.insert(coreids, cores["100 ton core"].id)
-- 50 t core
cores["200 ton core"] = {}
	cores["200 ton core"].Model = "models/props_wasteland/laundry_washer003.mdl"
	cores["200 ton core"].Mass = 5000
	cores["200 ton core"].TonGrade = 200
	cores["200 ton core"].id = "200 ton core"
	
	table.insert(coreids, cores["200 ton core"].id)
-- 100 t core
cores["400 ton core"] = {}
	cores["400 ton core"].Model = "models/props_wasteland/horizontalcoolingtank04.mdl"
	cores["400 ton core"].Mass = 10000
	cores["400 ton core"].TonGrade = 400
	cores["400 ton core"].id = "400 ton core"
	
	table.insert(coreids, cores["400 ton core"].id)

if CLIENT then
	language.Add( "Tool_sitptool_name", "Space Is The Place - Core" )
	language.Add( "Tool_sitptool_desc", "Spawn a Core for Space Is The Place" )
	language.Add( "Tool_sitptool_0", "Left click to spawn the SITP Core Entity" )
	
	language.Add( "Undone_sitp_core", "Undone SITP Core" )
	language.Add( "SBoxLimit_sitp_core", "You've reached the SITP Core limit!" )
	
	
end

function TOOL:LeftClick ( trace )
	if CLIENT then return true end
	
	local ply = self:GetOwner()
	local coretype = self:GetClientInfo( "coretype" )
	
	if (!(table.HasValue(coreids, coretype))) then 
		print ("No valid core found! Coretype: " .. coretype)
		return false 
	end
	local getcore = cores[coretype]
	
	local SpawnPos = trace.HitPos + trace.HitNormal * 64
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	
	local Mass = getcore.Mass
	local TonGrade = getcore.TonGrade
	local Model = getcore.Model
	
	local ent = SITP_MakeCore( ply, Ang, SpawnPos, Mass, TonGrade, Model )
	
	undo.Create("sitp_core")
		undo.AddEntity( ent )
		undo.SetPlayer( ply )
	undo.Finish()
	ply:AddCleanup( "sitp_core", ent )
end
if SERVER then
	AddCSLuaFile("sitptool.lua")
	CreateConVar('sbox_maxsitp_core', 4)

	function SITP_MakeCore( ply, Ang, Pos, Mass, TonGrade, Model)
		if ( !ply:CheckLimit( "sitp_core" ) ) then return nil end
		local ent = ents.Create( "sitp_core" )
		ent.Model = Model
		ent.TonGrade = TonGrade
		ent.Mass = Mass
		ent.Owner = ply
		ent:SetPos(Pos)
		ent:SetAngles(Ang)
		
		ent:Spawn()
		
		ply:AddCount( "sitp_core", ent )
		return ent
	end

	duplicator.RegisterEntityClass("sitp_core", SITP_MakeCore, "Ang", "Pos", "Mass", "TonGrade", "Model", "Vel", "aVel", "frozen")
end

function TOOL:RightClick ( trace )

end

function TOOL:Reload ( trace )

end

function TOOL:UpdateGhostCore( ent, player )
	if (!ent) then return end
	if (!ent:IsValid() ) then return end
	
	local tr = utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	ent:SetPos( trace.HitPos + trace.HitNormal * 64 )
	ent:SetAngles( Ang )
	ent:SetNoDraw( false )
end

function TOOL:Think()
	local coretype = self:GetClientInfo("coretype")
	if(!(table.HasValue(coreids, coretype))) then return end
	
	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != cores[coretype].Model) then
            self:MakeGhostEntity( cores[coretype].Model, Vector(0,0,0), Angle(0,0,0) )
       end
	   
	self:UpdateGhostCore( self.GhostEntity, self:GetOwner() )
end

-- Tool.BuildCPanel
if CLIENT then
language.Add("Core_Type", "Type of Core" )

function TOOL.BuildCPanel( CPanel )
	CPanel:ClearControls()

	CPanel:AddControl( "Header", { Text = "Space Is The Place Core", Description = "Creates cores" } )
	CPanel:AddControl( "Label", { Text = "This tool creates Space Is The Place cores. These cores are necessary to survive in outer space." }  )
	
	local Options = { Default = { coretype = "1" } }
	local CVars = { "coretype" }
	CPanel:AddControl( "ComboBox", {Label = "#Core_Type", MenuButton = "0", Options = list.Get( "SITP_CoreTypeList" ) } )
	
	CPanel:AddHeader()
	CPanel:AddDefaultControls()		
end
list.Set( "SITP_CoreTypeList", "20 ton core", { sitptool_coretype = "20 ton core" } )
list.Set( "SITP_CoreTypeList", "100 ton core", { sitptool_coretype = "100 ton core" } )
list.Set( "SITP_CoreTypeList", "200 ton core", { sitptool_coretype = "200 ton core" } )
list.Set( "SITP_CoreTypeList", "400 ton core", { sitptool_coretype = "400 ton core" } )

end